<html>
<head>
<link rel="stylesheet" href="moneystyle.css">

<link type="text/css" href="../css/smoothness/jquery-ui-1.8.11.custom.css" rel="Stylesheet" />	
<script type="text/javascript" src="../js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui-1.8.11.custom.min.js"></script>
<script>
	var paymentplans = new Array(<?php require 'paymentplandata.php';?>);
	$(function() {
		$( "#selectpaymentplan" ).autocomplete({
			source: paymentplans,//"testdata.php",
			minLength: 2,
			
			select: function( event, ui ) {
				//console.log(event);
				document.getElementById('hidepaymentplan').value=ui.item.value;
				//alert(ui.item.value);
			} 
		});
	});
</script>
<script>
	var students = new Array(<?php require '../studentdata.php';?>);
	$(function() {
		$( "#whorecieve" ).autocomplete({
			source: students,//"testdata.php",
			minLength: 2,
			
			select: function( event, ui ) {
				//console.log(event);
				document.getElementById('hidewhorecieve').value=ui.item.value;
				//alert(ui.item.value);
			} 
		});
	});
</script>
</head>

<body>
	<div >
	
	<form action="assignplan.php" method="post">
		<div>
			<h3>Select payment plan</h3>
			<div class="ui-widget">
				<input type="text" id="selectpaymentplan" />
			</div>
			
			<div>
				<input type="hidden" id="hidepaymentplan" name="selectedpaymentplan">
			</div>
		</div>
		
		<div>
			<h3>Select who</h3>
			<div class="ui-widget">
				<input type="text" id="whorecieve" />
			</div>
			
			<div>
				<input type="hidden" id="hidewhorecieve" name="selectedwhorecieve">
			</div>
		</div>
		
		<div class="ui-widget">
				<input type="submit"/>
		</div>
	</form>
	<?php 
	
	if(isset($_POST['selectedpaymentplan']) && isset($_POST['selectedwhorecieve'])){
		$paymentplan = &$_POST['selectedpaymentplan'];
		$whorecieve = &$_POST['selectedwhorecieve'];
		
		$paymentquery = "SELECT * FROM paymentplan WHERE name=\"".$paymentplan."\"";
		$whoquery = "SELECT * FROM students WHERE nimi=\"".$whorecieve."\"";

		$resultpaymentplan = mysql_query($paymentquery); // muutastudents
		$resultwhorecieve  = mysql_query($whoquery); // muutastudents
		
		$rowpaymentplan = mysql_fetch_array($resultpaymentplan);
		$rowwhorecieve = mysql_fetch_array($resultwhorecieve);
		//echo $rowpaymentplan['id']."   ".$rowwhorecieve['id']."added n stuff";

		$studentpayment = sprintf("INSERT INTO paymentstudent (paymentplan_id, student_id)
		VALUES ('%s','%s');" ,
		mysql_escape_string($rowpaymentplan['id']), 
		mysql_escape_string($rowwhorecieve['id'])
		);
		mysql_query($studentpayment);
	}

	?>
	<a href="Money.php">BACK</a>
	
	
	</div>
</body>
</html>








